package com.ww.crawler.webmagic.pipeline;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

public class ImgPipeline extends FilePersistentBase implements Pipeline {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private static int count = 0;
	
	public ImgPipeline() {
		setPath("/Users/wangwei/jointour_img");
		logger.info("图片保存在:{}", this.getPath());
	}
	
	public ImgPipeline(String path ) {
		setPath("/Users/wangwei/jointour_img");
	}
	
	@Override
	public void process(ResultItems resultItems, Task task) {
		String fileStorePath = this.path;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		for(Map.Entry<String, Object> entry : resultItems.getAll().entrySet()){
			if(entry.getValue() instanceof List){
				List<?> listOne = (List<?>) entry.getValue();
				List<String> list = new ArrayList<String>();
				for(int i=0; i<listOne.size(); i++){
					list.add((String) listOne.get(i));
				}
				for (int i=1; i<list.size(); i ++) {
					Path target = Paths.get(fileStorePath);
					if(!Files.isReadable(target)){
						try {
							Files.createDirectory(target);
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					String extName = com.google.common.io.Files.getFileExtension(list.get(i));
					String fileName = com.google.common.io.Files.getNameWithoutExtension(list.get(i));
					String imgFilePath = fileStorePath + fileName + "." + extName;
					HttpGet httpget = new HttpGet(list.get(i));
					HttpResponse response;
					try {
						response = httpclient.execute(httpget);
						HttpEntity entity = response.getEntity();
						File file = new File(imgFilePath);
						if(file.exists()){
							logger.info(">>>>>>>> {} 该文件已存在", file.getName());
							break;
						}
						InputStream in = entity.getContent();
						try{
							FileOutputStream fout = new FileOutputStream(file);
							int l = -1;
							byte[] tmp = new byte[1024];
							while((l = in.read(tmp)) != -1){
								fout.write(tmp, 0, l);
							}
							fout.flush();
							fout.close();
						} finally {
							logger.info("图片{} - 已完成：{}", list.get(i), ++count);
							in.close();
						}
					} catch (ClientProtocolException e) {
						logger.warn("图片{} - 下载失败 - exception {}", list.get(i), e.getMessage());
						e.printStackTrace();
					} catch (IOException e) {
						logger.warn("图片{} - 下载失败 - exception {}", list.get(i), e.getMessage());
						e.printStackTrace();
					} 
				}
			} else {
				logger.error(entry.getKey() + ":\t" +entry.getValue());
			}
		}
		try {
			httpclient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
